package com.guddqs.monkey.shop.user.web;

import com.guddqs.base.BaseController;
import com.guddqs.base.MapBean;
import com.guddqs.monkey.shop.user.entity.UserModel;
import com.guddqs.monkey.shop.user.service.IUserService;
import com.guddqs.monkey.shop.utils.MailService;
import com.guddqs.util.MD5Util;
import com.guddqs.util.MathUtil;
import com.guddqs.util.StringUtil;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;


/**
 * @author generator-wq
 * @date 2018/12/07 11:40:05
 */
@RestController
@RequestMapping("/api/user")
@Api(description = "用户")
public class UserController extends BaseController {

    @Resource
    private IUserService userServiceImpl;
    @Resource
    private MailService mailService;

    @PostMapping("/findAll")
    public MapBean findAll() throws Exception {
        return success(userServiceImpl.findAll());
    }

    @PostMapping("/login")
    public MapBean login(String loginName, String pwd, HttpSession session) {
        UserModel userModel = userServiceImpl.selectOne(new UserModel(loginName));
        if (userModel == null) {
            userModel = userServiceImpl.selectOne(new UserModel(loginName, 0));
            if (userModel == null) {
                userModel = userServiceImpl.selectOne(new UserModel(loginName, 1));
            }
        }
        if (userModel != null) {
            if (!StringUtil.isBlank(pwd) && MD5Util.getMD5(userModel.getLoginName() + "-" + pwd).equals(userModel.getPassword())) {
                session.setAttribute("login_user", userModel);
                userModel.setPassword(null);
                return success(userModel);
            } else {
                return error("用户名或密码不正确");
            }
        } else {
            return error("用户不存在");
        }
    }

    /**
     * 注销(猴哥专属)
     *
     * @param session 会话
     */
    @PostMapping("/logout")
    public MapBean logout(HttpSession session) {
        Object loginUser = session.getAttribute("login_user");
        if (loginUser != null) {
            session.invalidate();
            return success();
        } else {
            return error("未登录", 2);
        }
    }

    @PostMapping("/findUser")
    public MapBean findUser(HttpSession session) {
        Object loginUser = session.getAttribute("login_user");
        if (loginUser != null) {
            UserModel userModel = (UserModel) loginUser;
            userModel.setPassword(null);
            return success(userModel);
        } else {
            return error("未登录", 2);
        }
    }

    @PostMapping("/regist")
    public synchronized MapBean regist(String email, String phone, String loginName, String pwd, HttpSession session) throws Exception {
        if (!StringUtil.isBlank(loginName)) {
            UserModel userModel = userServiceImpl.selectOne(new UserModel(loginName));
            if (userModel == null) {
                userModel = getInsertUser(loginName, pwd, 0);
                userServiceImpl.insert(userModel);
                return success(userModel);
            } else {
                return error("用户已存在");
            }
        } else if (!StringUtil.isBlank(email)) {
            UserModel userModel = userServiceImpl.selectOne(new UserModel(email, 0));
            if (userModel == null) {
                userModel = getInsertUser(email, pwd, 1);
                userServiceImpl.insert(userModel);
                return success(userModel);
            } else {
                return error("用户已存在");
            }
        } else if (!StringUtil.isBlank(phone)) {
            UserModel userModel = userServiceImpl.selectOne(new UserModel(phone, 1));
            if (userModel == null) {
                userModel = getInsertUser(phone, pwd, 2);
                userServiceImpl.insert(userModel);
                return success(userModel);
            } else {
                return error("用户已存在");
            }
        } else {
            return error("no name");
        }
    }

    private UserModel getInsertUser(String data, String pwd, int type) {
        UserModel userModel = new UserModel();
        String loginName;
        if (type == 0) {
            loginName = data;
        } else {
            loginName = System.currentTimeMillis() + "";
            loginName = "g" + loginName.substring(7, 13) + MathUtil.randomInt(1, 10);
            if (type == 1) {
                userModel.setEmail(data);
                mailService.sendSimpleMail(data, "[猴哥商城] - 注册成功通知!", "<h1>我最最尊贵的用户您好, 欢迎您注册我们的网站, 也欢迎您...... <em>我 下去卍编布 了<em></h1>");
            } else if (type == 2) {
                userModel.setPhone(data);
            }
        }
        userModel.setLoginName(loginName);
        userModel.setNickName(loginName);
        userModel.setPassword(MD5Util.getMD5(loginName + "-" + pwd));
        userModel.setStatus(1);
        return userModel;
    }


}